/*
 * Copyright (c) create by xjs 16-11-17 下午5:54
 */

package com.example.commonlib.commonutil;

import android.view.View;

import com.example.commonlib.R;

/**
 * 时间触发间隔用于点击事件
 * <p>
 * Created by xjs on 2016/11/17.
 */
public class EventEmitUtil {
    public static final long DEFAULT_RESPONSE_EMIT_INTERVAL = 800;

    /**
     * 事件间隔
     * 默认响应时间 800毫秒
     *
     * @param view click view
     * @return
     */
    public static boolean checkIsResponse(View view) {

        if (view == null) {
            return false;
        }
        long nowTime = System.currentTimeMillis();
        Object tag = view.getTag(R.id.event_emit);
        if (tag == null) {
            view.setTag(R.id.event_emit, nowTime);
            return true;
        }

        long lastEmitTime = (long) tag;
        if (nowTime - lastEmitTime > DEFAULT_RESPONSE_EMIT_INTERVAL) {
            view.setTag(R.id.event_emit, nowTime);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 点击间隔
     *
     * @param view                  间隔载体
     * @param clickResponseInterval 间隔时间
     * @return
     */
    public static boolean checkIsResponse(View view, long clickResponseInterval) {

        if (view == null) {
            return false;
        }
        Object tag = view.getTag(R.id.event_emit);
        long nowTime = System.currentTimeMillis();
        if (tag == null) {
            view.setTag(R.id.event_emit, nowTime);
            return true;
        }
        long lastEmitTime = (long) tag;
        if (nowTime - lastEmitTime > clickResponseInterval) {
            view.setTag(R.id.event_emit, nowTime);
            return true;
        } else {
            return false;
        }
    }
}
